package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.common.ListUtil;
import org.easy.tool.common.TimeUtil;
import org.easy.tool.common.WebUtil;
import org.easy.tool.entity.UserDetail;
import org.easy.tool.mapper.UserDetailMapper;
import org.easy.tool.service.IUserDetail;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Valley
 * @since 2025-07-19 18:57:11
 */
@Service
public class UserDetailService extends ServiceImpl<UserDetailMapper, UserDetail> implements IUserDetail {

    @Autowired
    UserDetailMapper userDetailMapper;

    public static final String TAG = "tag";
    public static final String MISTAKE = "mistake";

    @Override
    public Object getUserTagList(LinkedHashMap data) {
        LambdaQueryWrapper<UserDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDetail::getType, TAG).eq(UserDetail::getUserId, WebUtil.getUserId());
        List<UserDetail> list = list(wrapper);
        list.sort(Comparator.comparingInt(UserDetail::getSort));
        return list.stream().map(UserDetail::getDetailId).collect(Collectors.toList());
    }

    @Override
    public Object updateUserTagList(LinkedHashMap data) {
        List<String> tags = (List<String>) data.get("tags");
        String userId = WebUtil.getUserId();
        LambdaQueryWrapper<UserDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDetail::getUserId, userId).eq(UserDetail::getType, TAG);
        List<UserDetail> list = list(wrapper);
        for (UserDetail userDetail : list) {
            removeById(userDetail.getId());
        }
        //tagId其实就是knowledgeId
        for (int i = 0; i < tags.size(); i++) {
            UserDetail userDetail = new UserDetail();
            userDetail.setType(TAG);
            userDetail.setUserId(WebUtil.getUserId());
            userDetail.setDetailId(tags.get(i));
            userDetail.setSort(i + 1);
            userDetail.setCreateTime(TimeUtil.nowMS());
            userDetail.setUpdateTime(TimeUtil.nowMS());
            save(userDetail);
        }
        return true;
    }

    @Override
    public boolean markUserProblemDetail(String userId, String problemId, String type) {
        UserDetail userDetail = getUserProblemDetail(userId, problemId, type);
        //取消标记
        if (userDetail != null) {
            removeById(userDetail);
            return true;
        }
        //增加标记
        userDetail = new UserDetail();
        userDetail.setDetailId(problemId);
        userDetail.setType(type);
        userDetail.setUserId(WebUtil.getUserId());
        userDetail.setCreateTime(TimeUtil.nowMS());
        userDetail.setUpdateTime(TimeUtil.nowMS());
        save(userDetail);
        return true;
    }

    @Override
    public UserDetail getUserProblemDetail(String userId, String problemId, String type) {
        LambdaQueryWrapper<UserDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDetail::getUserId, userId).eq(UserDetail::getType, type)
                .eq(UserDetail::getDetailId, problemId);
        return getOne(wrapper);
    }
}
